using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace EARogueCalc
{
    public partial class ItemCompare : Form
    {
        public ItemCompare()
        {
            InitializeComponent();
            CheckItemList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
//todo errprovider, load web
            string itemId = Statics.GetItemId(textBox1.Text.Trim());
            if (itemId == Statics.ITEM_DEFAULT_NONITEM)
                itemBrowser1.DocumentText =
                    "Item not found.  Please check spelling and case and try again, or refresh the item database";
            else
            {
                try
                {
                    itemBrowser1.Navigate(Statics.ITEM_LITE_HTML_URL + itemId);
                }
                catch
                {
                    itemBrowser1.DocumentText = Statics.ITEM_LITE_HTML_URL + itemId;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string itemId = Statics.GetItemId(textBox2.Text.Trim());
            if (itemId == Statics.ITEM_DEFAULT_NONITEM)
                itemBrowser2.DocumentText =
                    "Item not found.  Please check spelling and case and try again, or refresh the item database";
            else
                itemBrowser2.DocumentText = Statics.GetHTML(Statics.ITEM_LITE_HTML_URL + itemId);
        }

        private void CheckItemList()
        {
            if (!File.Exists(Statics.ITEM_LIST_PATH))
            {
                GetAllakhazamItemList();
            }
        }

        private void GetAllakhazamItemList()
        {
            MessageBox.Show("Please wait while we initialize the item list from Allakhazam");
            ToggleUIEnable(false);
            XmlDocument itemList = Statics.GetFeed(Statics.ITEM_LIST_XML_URI);
            itemList.Save(Statics.ITEM_LIST_PATH);
            ToggleUIEnable(true);
        }

        private void ToggleUIEnable(bool enabled)
        {
            textBox1.Enabled = enabled;
            textBox2.Enabled = enabled;
            button1.Enabled = enabled;
            button2.Enabled = enabled;
        }
    }
}